package com.geotmt.billingcenter.modules.cost.mapper.provider;

import com.geotmt.billingcenter.common.entry.CostBaseHisEntry;
import com.geotmt.billingcenter.common.vo.params.ParamsCost;
import com.geotmt.billingcenter.common.vo.params.ParamsForDel;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @Description 类描述
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/20 13:40
 * @Version     v1
 **/
public class CostBaseHisProvider extends WhereProvider {

    private static final Logger logger = LoggerFactory.getLogger(CostBaseHisProvider.class);

    private static final int NORMAL_STATUS= 1;

    private static final int DELETE_STATUS= 2;

    /**
     * 添加历史成本Base
     * @return
     */
    public String add(CostBaseHisEntry costBaseHis){
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO `fin_cost_his_conf_base`")
                .append(" (ds_id,out_interface_id,fee_flag,effect_start_time,effect_end_time,")
                .append(" create_user_id,create_time,`status`,`desc`,label,r_id)")
                .append(" VALUES")
                .append(" (#{dsId},#{outInterfaceId},#{feeFlag},#{effectStartTime},#{effectEndTime},")
                .append(" #{createUserId},#{createTime},"+ NORMAL_STATUS +",#{desc},#{label},#{rId})");
        logger.info("历史成本Base add,SQL:{}",sql);
        return sql.toString();
    }

    /**
     * 删除历史成本 Base
     * @return
     */
    public String delete(ParamsForDel obj){
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE `fin_cost_his_conf_base` SET update_user_id=#{updateUserId},update_time=#{updateTime},status=" + DELETE_STATUS + " WHERE id=#{id}");
        logger.info("历史成本Base delete,SQL:{}",sql);
        return sql.toString();
    }

    /**
     * 历史成本 lsit
     * @param obj
     * @return
     */
    public String list(ParamsCost obj){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT currHis.id,currHis.ds_id dsId,currHis.out_interface_id interfaceId,currHis.fee_flag feeFlag,")
                .append(" currHis.effect_start_time effectStartTime,currHis.`effect_end_time` effectEndTime,currHis.create_user_id createUserId,currHis.create_time createTime,currHis.update_user_id updateUserId,")
                .append(" currHis.update_time updateTime,currHis.`status`,currHis.`desc`,")
                .append(" ruleHis.price,ruleHis.`id` rId,ruleHis.`stair_cycle` stairCycle, ruleHis.`stair_start_time` stairStartTime,")
                .append(" ds.name dsName,ds.ds_full_name dsFullName,")
                .append(" inter.interface_name outInterfaceName,")
                .append(" cuserCre.real_name,")
                .append(" cuserUp.real_name")
                .append(" FROM `fin_cost_his_conf_base` currHis")
                .append(" LEFT JOIN `fin_cost_curr_conf_rule` ruleHis ON currHis.r_id=ruleHis.id")
                .append(" LEFT JOIN `t_ds_info` ds on currHis.ds_id=ds.id")
                .append(" LEFT JOIN `t_out_interface` inter ON currHis.`out_interface_id`=inter.`id`")
                .append(" LEFT JOIN `sys_user` cuserCre ON currHis.`create_user_id`= cuserCre.`id`")
                .append(" LEFT JOIN `sys_user` cuserUp ON currHis.`update_user_id`=cuserUp.`id`")
                .append(" WHERE 1=1");
        whereCost(sql,"currHis", obj);
        logger.info("历史成本 list,SQL:{}",sql);
        return sql.toString();
    }

    /**
     * 更新历史表中的有效结束时间
     * @param preDay
     * @param id
     * @return
     */
    public String updateEndEffect(@Param(value = "preDay") Date preDay,
                                  @Param(value = "id") Integer id){
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE `fin_cost_his_conf_base` SET effect_end_time=#{preDay} where id=#{id}");
        return sql.toString();
    }
}
